<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="活动名称">
              <a-input v-model="queryParam.subject" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchBtn">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => this.queryParam = { status: '' }">重置</a-button>
              <a-button style="margin-left: 8px" type="danger" :disabled="syncState" :loading="syncState" @click="syncBtn">同步</a-button>
              <a-button style="margin-left: 8px" @click="updateCode">一键更新签到签退</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :row-key="
        (record) => {
          return record.activityId
        }
      "
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <!-- <span slot="action" slot-scope="text, record">
        <router-link :to="{ name: 'ActivityEdit', params: { id: record.activityId } }">修改</router-link>
        <template v-if="record.isSignup == 1">
        | <router-link :to="{ name: 'ActivitySignList', params: { id: record.activityId } }">报名列表</router-link>
        </template>
      </span> -->
    </a-table>
  </a-card>
</template>
<script>
import { Modal } from 'ant-design-vue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'activityId'
  },
  {
    title: '活动编号',
    dataIndex: 'missionId'
  },
  {
    title: '活动名称',
    dataIndex: 'subject'
  },
  {
    title: '活动类型',
    dataIndex: 'missionTypeName'
  },
  {
    title: '联系人',
    dataIndex: 'contactName'
  },
  {
    title: '联系电话',
    dataIndex: 'contactPhone'
  },
  {
    title: '活动标签',
    dataIndex: 'selectedLabelIdListName'
  },
  {
    title: '活动时间',
    dataIndex: 'startDate',
    customRender: (text, record) => {
      return record.startDate + ' - ' + record.endDate
    }
  }
  // {
  //   title: '操作',
  //   key: 'action',
  //   scopedSlots: { customRender: 'action' }
  // }
]

export default {
  name: 'GdActivityList',
  data() {
    return {
      id: '',
      columns,
      loading: false,
      data: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (
            <div>
              共 <span>{total}</span> 条
            </div>
          )
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      },
      queryParam: {},
      actives: [],
      syncState: false // 同步状态
    }
  },
  mounted() {
    // 获取列表
    this.fetch()
  },
  methods: {
    // 改变每页数量时更新显示
    onChange(pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn() {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch() {
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize
      }
      this.$common
        .handleGet('/gdzyz/lists', Object.assign({}, params, this.queryParam))
        .then((data) => {
          this.loading = false
          if (data.code === 200) {
            const result = data.data
            const pagination = { ...this.pagination }
            pagination.total = result.totalInfo.total
            this.data = result.lists
            this.pagination = pagination
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    // 同步
    syncBtn() {
      this.syncState = true

      const modal = Modal.warning()
      modal.update({
        title: '信息同步中...',
        content: '信息同步中，请勿关闭页面！！！'
      })

      this.$common.handlePost('http://api.zywm.expohub.cn/admin/gdzyz/synchro').then(data => {
        this.syncState = false
        modal.destroy()
        if (data.code === 200) {
          this.$message.success(data.msg)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 一键更新签到签退
    updateCode() {
      window.open('http://api.zywm.expohub.cn/applet/test/credit')
    }
  }
}
</script>
